package com.free.common.vo;

import com.free.common.domain.ComMessageCodes;
import com.free.messager.MessageUtils;

import java.io.Serializable;

/**
 * <p>
 * 功能概述:
 * </p>
 * <p>
 * 功能详述:
 * </p>
 */
public class ResultModel implements Serializable {

    /*========================================================================*
     *                         Public Fields (公共属性)
     *========================================================================*/

    /*========================================================================*
     *                         Private Fields (私有属性)
     *========================================================================*/

    private static final long serialVersionUID = -1541291875749988793L;
    private boolean success = true;

    private String message;

    private Enum messageCode;

    private Object data;

    /*========================================================================*
     *                         Construct Methods (构造方法)
     *========================================================================*/

    public ResultModel(boolean success, Object obj, Enum messageCode, Object... params) {
        String message = MessageUtils.getMessage(messageCode, params);
        this.success = success;
        this.message = message;
        this.data = obj;
        this.messageCode = messageCode;
    }

    public ResultModel(boolean success, String message, Object obj){
        this.success = success;
        this.message = message;
        this.data = obj;
    }

    /*========================================================================*
     *                         Public Methods (公有方法)
     *========================================================================*/

    public static ResultModel success(){
        return success(null);
    }

    public static ResultModel success(Enum messageCode, Object... params){
        return success(null, messageCode, params);
    }

    public static  ResultModel success(Object obj){
        return success(obj, ComMessageCodes.S000001);
    }

    public static ResultModel success(Object obj, Enum messageCode, Object... params) {
        return new ResultModel(true, obj, messageCode, params);
    }

    public static ResultModel success(Object obj, String message){
        return new ResultModel(true, message, obj);
    }

    public static ResultModel failure() {
        return failure(null);
    }

    public static ResultModel failure(Enum messageCode, Object... params){
        return failure(null, messageCode, params);
    }

    public static ResultModel failure(Object obj, Enum messageCode, Object... params) {
        return new ResultModel(false, obj, messageCode, params);
    }

    public static ResultModel failure(Object obj){
        return failure(obj, ComMessageCodes.E000001);
    }

    public static ResultModel failure(Object obj, String message){
        return new ResultModel(false, message, obj);
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Enum getMessageCode() {
        return messageCode;
    }

    public void setMessageCode(Enum messageCode) {
        this.messageCode = messageCode;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultModel{" +
                "success=" + success +
                ", message='" + message + '\'' +
                ", messageCode=" + messageCode +
                ", data=" + data +
                '}';
    }

    /*========================================================================*
     *                         Private Methods (私有方法)
     *========================================================================*/
}
